#include "oToDay.h"

#include <fstream>

using namespace std;

QString readWP(){
    QString p, ret;
    string stream;
    p=QDir::homePath()+"/.opendesk/config/look";
    qWarning(p.toStdString().c_str());
    ifstream fin(p.toStdString().c_str());
    while (fin >> stream) {
        if (stream[0]=='#') {
            qWarning(stream.c_str());
        }
        if (stream=="WP") {
            fin >> stream;
            p = QString::fromStdString(stream);
            return p;
        }
    }
    p="/usr/pixmaps/opendesk/wp.png";
    return p;
}

oToDay::oToDay(QWidget * parent) : QWidget(parent) {
    //window settings
    setGeometry(00,0,480,640);
    this->setWindowTitle("oDesk");
    this->setWindowFlags( Qt::FramelessWindowHint);
    //Wallpaper;
    wallpaper = readWP();
    //Prefered apps bar object
    bar = new QBar(this);
    bar->setGeometry(3,547,474, 90);
    connect(bar->appsIcon, SIGNAL(clicked()), this, SLOT(appsClick()));
  //  line = new QELineEdit(this);
  //  line->setGeometry(20,60,440,22);
    //WIDGET EXAMPLE
    QEWidget  * widget = new QEWidget(true,true,1,this);
    widget->setGeometry(11,130,456,100);
    widget->show();
    QEWidget  * widget2 = new QEWidget(true,false,1,this);
    widget2->setGeometry(11,130,456,100);
    widget2->show();
    QEWidget  * widget3 = new QEWidget(false,true,1,this);
    widget3->setGeometry(11,240,456,150);
    widget3->show();
    QEWidget  * widget4 = new QEWidget(false,true,1,this);
    widget4->setGeometry(11,400,456,75);
    widget4->show();
    //Widget menu set up
    widgetMenu = new QEMenu(this);
    QAction *del= new QAction(this);  //delete widget
    del->setText("Delete widget");
    QAction *prop= new QAction(this); // modify the property of the widget
    prop->setText("Propery");
    QAction *back= new QAction(this); // turn back at the home screen
    back->setText("Back at home");
    QAction *newW= new QAction(this); // add a new Widget
    newW->setText("New widget");
    QAction *exit= new QAction(this); // shutdown dialog
    exit->setText("Shutdown");
    //Add actions at the menu
    widgetMenu->addItem(prop);
    widgetMenu->addItem(newW);
    widgetMenu->addItem(del);
    widgetMenu->addItem(back);
    widgetMenu->addItem(exit);
    widgetMenu->hide();
    //var init and signal connect
    ptime=0;
    connect(back, SIGNAL(triggered()), widgetMenu, SLOT(hide()));
    connect(exit, SIGNAL(triggered()), this, SLOT(close()));
    connect(this, SIGNAL(longPress()), widgetMenu, SLOT(show()));
}

oToDay::~oToDay() {}

void oToDay::paintEvent(QPaintEvent * event) {
    QPainter p (this);

    p.setRenderHint(QPainter::TextAntialiasing);
    p.setRenderHint(QPainter::Antialiasing);

    QColor darkBlack(16,16,16,255);

    p.setBrush(darkBlack);
    p.setPen(darkBlack);

    p.drawRect(0,0,480,640);

    p.save();
    p.drawImage(3,25,QImage(wallpaper));
    p.restore();

    p.end();

}

void oToDay::appsClick(){    
  /*  QEMenu * menu = new QEMenu(this);
    menu->show();
    QAction * c = new QAction(this);
    c->setText("Impostazioni");
    QAction * a = new QAction(this);
    a->setText("Esci");
    QAction * b = new QAction(this);
    b->setText("Nascondi");
    menu->addItem(c);
    menu->addItem(b);
    menu->addItem(a);
    connect(b, SIGNAL(triggered()), menu, SLOT(hide()));
    connect(a, SIGNAL(triggered()), this, SLOT(close()));*/
}

void oToDay::mousePressEvent(QMouseEvent * e){
    timer = new QTimer();
    timer->start(600);
    ptime= 0;
    connect(timer, SIGNAL(timeout()), this, SLOT(tupdate()));
    qWarning("Mouse Pressed\n");
}

void oToDay::mouseReleaseEvent(QMouseEvent *e) {
    if (ptime>=2) {
        emit longPress();
//        this->close();

        qWarning("Mouse Pressed for more then 1.2 sec\n");
    }
    ptime=0;
    timer->stop();
    delete timer;
}

void oToDay::tupdate(){
    ptime++;
    qWarning("Timer update\n");
}
